/**
 * 
 */
package com.wskj.library.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wskj.library.pojo.Employer;
import com.wskj.library.service.EmployerService;
import com.wskj.library.service.impl.EmployerServiceImpl;

/**
 * @author 胡洋
 * @describe 
 * @Date 2018年3月26日下午4:29:40
 * @version 1.0
 */
@WebServlet(urlPatterns="/resetPass.do")
public class ResetPassword  extends HttpServlet{
	EmployerService emService=new EmployerServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.getRequestDispatcher("/WEB-INF/page/updatePassword.jsp").forward(req, resp);
	}
	
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		String oldPass = req.getParameter("oldPass");
		String newPass = req.getParameter("newPass");
		String reNewPass = req.getParameter("reNewPass");
		
		//判断用户输入的密码是否正确：
		Employer user =(Employer) req.getSession().getAttribute("user");
		
		
		if(oldPass.equals(user.getE_pass())) {
			//二次密码输入的相同 ....
			if(newPass.equals(reNewPass)) {
				
				//修改用户密码的信息 .....
				boolean  result=emService.updatePassWord(user.getE_id(),newPass);
				if(result) {
					//重置成功 ...清空session 让用户重新登录 
					req.getSession().invalidate();
					//跳出框架 ......
					PrintWriter out = resp.getWriter();
					out.write("<script>alert('修改密码成功');top.location.href='"+req.getContextPath()+"/admin/login.do';</script>");
					/*resp.sendRedirect(req.getContextPath()+"/admin/login.do");*/
					return;
				}
				
				resp.sendRedirect(req.getContextPath()+"/resetPass.do");
			}
			resp.sendRedirect(req.getContextPath()+"/resetPass.do");
			
		}else {
			//旧密码不正确 ....直接返回 修改密码页面 
			resp.sendRedirect(req.getContextPath()+"/resetPass.do");
		}
		
	}

}
